package com.zxmr.client;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zxmr.action.QueueWithoutExchangeReceiver;
import com.zxmr.action.Request;
import com.zxmr.action.RequestPayload;
import com.zxmr.asyn.TaskMonitor;
import com.zxmr.domain.account.User;
import com.zxmr.exception.InvalidRequestMessage;

public class RabbitClient {

	
	public static void main(String[] argv)
		      throws java.io.IOException, InvalidRequestMessage {
		
		
		TaskMonitor monitor = new TaskMonitor(10,30);
		Thread t = new Thread(monitor);
		t.start();
		
		
		Request request = new Request();
		request.setNeedResponse(true);
		
		User employee = new User();
		employee.setName("yewenkuan");
		employee.setPassword("sfdfd");
		
		request.setAccount(employee);
		
		RequestPayload payload = new RequestPayload();
		payload.setUrl("/test");
		Map<String,String> parameters = new HashMap<String,String>();
		parameters.put("key", "value");
		payload.setParameters(parameters);
		
		request.setPayload(payload);
		request.setId("1111");

		
		/*
		ConnectionFactory factory = new ConnectionFactory();
		System.out.println("default virtual host is "+factory.getVirtualHost());
	    //factory.setHost("localhost");
		factory.setVirtualHost("/some");
	    Connection connection = factory.newConnection();
	    Channel channel = connection.createChannel();
	    
	    channel.exchangeDeclare("hello", "direct");
	    
	    //channel.queueDeclare(QUEUE_NAME, false, false, false, null);
	    String message = "Hello World!";
	    channel.basicPublish("helloWorld", "helloWorld", null, message.getBytes());
	    System.out.println(" [x] Sent '" + message + "'");
	    channel.close();
	    connection.close();
		 */
//		request.send("response", "hello");
		
		NormalAsynRequest asynRequest = new NormalAsynRequest();
		asynRequest.setRequest(request);
		asynRequest.send();
	    
		ExecutorService es = Executors.newCachedThreadPool();
		QueueWithoutExchangeReceiver response = new QueueWithoutExchangeReceiver(es,"response");

		es.execute(response);

		
	}
}
